<template>
  <div class="app-container">
    <el-row v-if="printConfig" style="display: flex;justify-content: right;">
      <print :disabled="dataList && dataList.length == 0" title="设备使用记录" id="print-use-record"
        style="margin-bottom: 15px;margin-top: -15px;" :beforeMethod="beforeMethod" :showPrintBtn="false">
        <table style="width: 100%" border="1" cellspacing="0" cellpadding="0" align="center">
          <thead>
            <tr>
              <td>设备名称</td>
              <td>{{ printConfig.equipmentName }}</td>
            </tr>
            <tr>
              <td>设备编号</td>
              <td>{{ printConfig.equipmentNumber }}</td>
            </tr>
          </thead>
        </table>
        <table style="width: 100%;margin-top: -2px;" border="1" cellspacing="0" cellpadding="0" align="center">
          <thead>
            <tr>
              <td rowspan="2">使用地点</td>
              <td rowspan="2">时间</td>
              <td rowspan="2">检测项目</td>
              <td rowspan="2">使用人</td>
              <td colspan="3">状态</td>
            </tr>
            <tr>
              <td>检测前</td>
              <td>检测时</td>
              <td>检测后</td>
            </tr>
          </thead>
          <tr v-for="(item, index) in printDataList" :key="index">
            <td>{{ item.resultType == 'DOSE' ? '实验室' : item.entrustOrganizationName }}</td>
            <td>{{ item.createTime }}</td>
            <td>{{ item.resultType == 'DOSE' ? item.personName : item.entrustEquipmentName }}</td>
            <td>{{ item.testerName }}</td>
            <td>{{ item.useBefore == 'NORMAL' ? '正常' : '损坏' }}</td>
            <td>{{ item.useThen == 'NORMAL' ? '正常' : '损坏' }}</td>
            <td>{{ item.useAfter == 'NORMAL' ? '正常' : '损坏' }}</td>
          </tr>
        </table>
      </print>
    </el-row>

    <div style="display: flex;align-items: center;justify-content: space-between; width: 100%; margin-bottom: 10px;">
      <div>

      </div>
      <div style="margin-left: auto;">
        <el-button type="warning" plain icon="el-icon-printer" size="mini"
          @click.native.prevent="handlePrint" :disabled="dataList.length == 0" v-if="showPrintBtn">打印</el-button>
      </div>
    </div>

    <el-table v-loading="loading" :data="dataList"
      @row-dblclick="(row) => openDialog(row, 'view')">
      <el-table-column prop="useDate" label="领用日期" align="center" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.useDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="entrustOrganizationName" label="检测单位" align="center" min-width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.entrustOrganizationName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="abbreviation" label="设备简称" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.abbreviation }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="" label="仪器状态" align="center">
        <el-table-column prop="useBefore" label="使用前" align="center" min-width="100">
          <template slot-scope="{ row }">
            <el-switch v-model="row.useBefore" active-color="#13ce66" inactive-color="#ff4949" active-value="NORMAL"
              @change="changeState(row, 'useBefore')" inactive-value="DAMAGE">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="useAfter" label="使用后" align="center" min-width="100">
          <template slot-scope="{ row }">
            <el-switch v-model="row.useAfter" active-color="#13ce66" inactive-color="#ff4949"
              @change="changeState(row, 'useAfter')" active-value="NORMAL" inactive-value="DAMAGE">
            </el-switch>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column prop="testerName" label="使用人" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.testerName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="maintenance" label="维护保养" align="center" min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.maintenance == '0'">否</span>
          <span v-else-if="scope.row.maintenance == '1'">是</span>
          <span v-else-if="scope.row.maintenance == '2'">清洁仪器外壳</span>
          <span v-else-if="scope.row.maintenance == '3'">充电</span>
        </template>
      </el-table-column>
      <el-table-column prop="maintenanceUserName" label="维护人" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.maintenanceUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="60">
        <template slot-scope="{row}">
          <el-link type="danger" @click.native.prevent="handleDelete(row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%;height: 50px;">
      <pagination v-show="total > 0" :total="total" :page.sync="page.pageNum" :limit.sync="page.pageSize"
        @pagination="getList" />
    </div>
    <user-record-dialog ref="dialog"></user-record-dialog>
  </div>
</template>

<script>
import { queryPageList, updateState, delRecordById, generateUseRecords } from '@/api/equipment/use'
import userRecordDialog from './useRecordDialog.vue'

export default {
  name: 'useRecordList',
  components: { userRecordDialog },
  props: {
    queryParams: {
      type: Object,
      default: () => {
        return {}
      }
    },
    printConfig: {
      type: Object | Boolean,
      default: false
    },
    showPrintBtn: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dataList: [],
      page: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      loading: false,
      //打印数据
      printDataList: []
    }
  },
  computed: {
    showType() {
      let flag = false
      this.dataList.forEach(t => {
        if (t.resultType != 'DOSE') {
          flag = true
        }
      })
      return flag
    }
  },
  methods: {
    changeState(row, state) {
      updateState({ id: row.id, usageTime: state.replace('use', '').toUpperCase(), usageState: row[state] })
    },
    getList() {
      this.$nextTick(() => {
        this.loading = true
        queryPageList({ ...this.queryParams, ...this.page }).then(res => {
          this.dataList = res.rows
          this.total = res.total
        }).finally(() => {
          this.loading = false
        })
      })
    },
    openDialog(row, type) {
      // this.$refs.dialog.open(row.equipmentId,type)
    },
    reset() {
      this.page.pageNum = 1;
    },
    handleDelete(row) {
      this.$confirm(`是否确认删除仪器[${row.equipmentName}]记录`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRecordById(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList()
        })
      });
    },
    async beforeMethod() {
      let res = await queryPageList({ ...this.queryParams, ...{ pageNum: 1, pageSize: this.total } })
      this.printDataList = res.rows
    },
    handlePrint() {
      generateUseRecords({ ...this.queryParams }).then(res => {
        if (res.msg) window.open(res.msg)
      }).catch(err => {})
    }
  }
}
</script>

<style lang="scss">
table {
  text-align: center;
}
</style>
